<?php
/**
 * Silverstripe cms tree  Extension
 * Powered by jQuery
 * @author rgodinho
 */
class SSCustomTreeListExtension extends Extension {
  	  	
	function extraStatics() {
    return array(
    );
  }

  public function Subject() {
    return $this->owner;
  }

  /**
   * Render a template
   * @param $template template_file
   * @return html render
   */
  public function Template($template) {
    return $this->owner->renderWith($template);
  }

   /**
   * Render Tree
   * @return html rendered
   */
  public function getSSCustomTreeList() {
		Requirements::javascript(SSCUSTOMTREELIST . '/javascript/jquery-1.4.min.js');
		Requirements::javascript(SSCUSTOMTREELIST . '/javascript/jquery-ui-1.7.2.custom.min.js');		
		Requirements::javascript(SSCUSTOMTREELIST . '/javascript/sscustomtreelist.js');

		// -- Extension css file
  		$this->includePluginsCss('sscustomtreelist.css');
  		
		return $this->owner->renderWith('SSCustomTreeList');
  }
      
  /**
   * include a css, check first in the the theme, then project, in the module or jsparty treeview
   * @param $css
   * @return void
   */
  private function includePluginsCss( $css ) {
    if( Director::fileExists($file = project() . '/themes/' . SSViewer::current_theme() . '/css/' . $css) ) {
      Requirements::css($file);

    }
    elseif( Director::fileExists($file = project() . '/css/' . $css) ) {
      Requirements::css($file);
    }
    else {    	
      Requirements::css(SSCUSTOMTREELIST . '/css/' . $css);
    }
  }  
}